package com.coocaa.ops.admin.v1.cornericon.service.impl;


import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.audit.AuditUtil;
import com.coocaa.ops.admin.v1.audit.model.ExecutionTaskModel;
import com.coocaa.ops.admin.v1.audit.service.impl.AuditTaskServiceImpl;
import com.coocaa.ops.admin.v1.cornericon.biz.impl.CornerIconBizImpl;
import com.coocaa.ops.admin.v1.cornericon.provider.impl.CornerIconUpdateProviderImpl;
import com.coocaa.ops.admin.v1.cornericon.service.ICornerIconUpdateService;
import com.coocaa.ops.admin.v1.cornericon.validator.CornerIconValidator;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.model.SimpleSysUserInfo;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/8/17.
 * @description
 */
@Service
public class CornerIconUpdateServiceImpl implements ICornerIconUpdateService {
    @Autowired
    private CornerIconBizImpl cornerIconBiz;
    @Autowired
    private CornerIconUpdateProviderImpl cornerIconUpdateProvider;
    private static CommonLanguageProperties commonLanguageProperties;


    //    @Autowired
//    private SendEmailServiceImpl sendEmailService;
    @Autowired
    private AuditTaskServiceImpl auditTaskService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBatch(List<GlobalCornerIconEntity> iconList, SysUserEntity user) {
        if (CollectionUtils.isNotEmpty(iconList)) {
            if (iconList.size() == 1 && iconList.get(0).getId() != null) {
                //更新
                updateIcon(iconList.get(0), user);
            } else {
                //新增
                addBatchIcon(iconList, user);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(GlobalCornerIconEntity entity, SysUserEntity user) {
        cornerIconUpdateProvider.save(entity, user.getId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map deleteBatch(Integer[] ids, SysUserEntity user) {
        String result = cornerIconUpdateProvider.deleteBatch(ids, user.getId());
        if (R.SUCCESS.equals(result)) {
            return R.ok().msg(commonLanguageProperties.getDeleteSuccess());
        } else {
            return R.fail(result);
        }
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        CornerIconUpdateServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchAudit(String cornerIconIds, SysUserEntity sysUserEntity, Integer auditFlag, String auditDesc) {
        if (StringUtils.isNotBlank(cornerIconIds)) {
            String[] iconIds = cornerIconIds.split(",");
            for (String iconId : iconIds) {
                //审核角标
                ExecutionTaskModel executionTaskModel = ExecutionTaskModel.getExecutionTaskModel(Integer.valueOf(iconId), null, ResourceTypeEnum.CORNER_ICON.toString(), auditFlag);
                executionTaskModel.setAuditDesc(auditDesc);
                //使用公用的审核服务类
                auditTaskService.auditTask(executionTaskModel, new SimpleSysUserInfo(sysUserEntity.getId(), sysUserEntity.getUserName()));
            }
        }
    }

    /**
     * 批量新增角标
     *
     * @param iconList
     * @param user
     */

    private void addBatchIcon(List<GlobalCornerIconEntity> iconList, SysUserEntity user) {
        for (GlobalCornerIconEntity entity : iconList) {
            CornerIconValidator.valid(entity);
            cornerIconUpdateProvider.save(entity, user.getId());
            AuditUtil.openTask(entity.getCornerIconName(), entity.getId(), null, ResourceTypeEnum.CORNER_ICON.toString(), null,
                    user, 1);
        }
        //送审
        //发送邮件通知审核人
//        if (SystemProperties.SystemConstants.ENABLE_SEND_EMAIL) {
//            sendEmailService.sentEmailForCornerIconBatch(iconList, user);
//        }
    }

    /**
     * 更新角标
     *
     * @param entity
     * @param user
     */
    private void updateIcon(GlobalCornerIconEntity entity, SysUserEntity user) {
        cornerIconUpdateProvider.save(entity, user.getId());
        this.sendToAudit(entity, user);
    }


    /**
     * 送审
     *
     * @param entity
     * @param user
     */
    private void sendToAudit(GlobalCornerIconEntity entity, SysUserEntity user) {
        AuditTaskInfoEntity taskInfoEntity = AuditUtil.openTask(entity.getCornerIconName(), entity.getId(), null, ResourceTypeEnum.CORNER_ICON.toString(), null,
                user, 1);
//        //发送邮件通知审核人
//        if (SystemProperties.SystemConstants.ENABLE_SEND_EMAIL) {
//            sendEmailService.sentEmailForCornerIcon(entity, user, taskInfoEntity);
//        }
    }


}
